//
//  NSObject+WLModelClass.m
//  tubaobao
//
//  Created by 专注 on 2017/8/23.
//  Copyright © 2017年 Rice. All rights reserved.
//

#import "NSObject+WLModelClass.h"
#import  <objc/runtime.h>

@implementation NSObject (WLModelClass)
/* 获取对象的所有属性 以及属性值 */
- (NSDictionary *)properties_model_class
{
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i<outCount; i++)
    {
        objc_property_t property = properties[i];
        const char* char_f =property_getName(property);
        NSString *propertyName = [NSString stringWithUTF8String:char_f];
        id propertyValue = [self valueForKey:(NSString *)propertyName];
        if (propertyValue) [props setObject:propertyValue forKey:propertyName];
        else [props setObject:@"" forKey:propertyName];
    }
    free(properties);
    return props;
}

- (NSArray *)getAllProperties_model_class
{
    u_int count;
    objc_property_t *properties  =class_copyPropertyList([self class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    
    /** 需要目标对象实现此方法 */
    NSArray *ignoredKeys = [self wl_classReturnMethod:@selector(ignoredKeyList)];
    for (int i = 0; i<count; i++)
    {
        const char* propertyName =property_getName(properties[i]);
        NSString *string = [NSString stringWithUTF8String: propertyName];
        if (ignoredKeys.count){
            if ([ignoredKeys containsObject:string]){
                continue;
            }
        }
        [propertiesArray addObject:string];
    }
    free(properties);
    return propertiesArray;
}


- (BOOL)isNullObj{
    id object = self;
    if (object == nil || [object isEqual:[NSNull null]]) {
        return YES;
    }
    else if ([object isKindOfClass:[NSString class]]) {
        if ([object isEqualToString:@""]) {
            return YES;
        }
        else if ([object rangeOfString:@"null"].location != NSNotFound) {
            return YES;
        }
        else{
            return NO;
        }
    }
    else if ([object isKindOfClass:[NSNumber class]]) {
        if ([object isEqualToNumber:@0]) {
            return YES;
        }
        else {
            return NO;
        }
    }
    return NO;
}


@end
